package com.im323.application;

import java.util.List;

import com.im323.dnspod.DnspodService;
import com.im323.dnspod.result.DnspodDomain;
import com.im323.dnspod.result.DnspodRecord;
import com.im323.dnspod.result.DnspodUser;
import com.im323.quartz.QuartzSimpleManager;
import com.im323.quartz.RecordUpdateJob;
import com.im323.quartz.UpdateRecordModel;

public class IMDDNSClient {

	public static void main(String[] args) {
		if (args == null || args.length <= 0) {
			showTip();
			return;
		}

		String command = args[0];
		String token = args[1];
		DnspodService.initBaseParam(token);
		if ("user".equals(command.toLowerCase())) {
			DnspodUser dnspodUser = IMDDNSUserController.getDnsPodUser();
			if (dnspodUser == null) {
				return;
			}
			System.out.println(dnspodUser);
		} else if ("domain".equals(command.toLowerCase())) {
			List<DnspodDomain> domainList = IMDDNSDomainController.getDomainList();
			if (domainList == null) {
				return;
			}
			System.out.println(String.format("%-10s|%-8s|%-10s", "域名ID", "状态", "备注"));
			for (int i = 0; i < domainList.size(); i++) {
				DnspodDomain dnspodDomain = domainList.get(i);
				System.out.println(String.format("%-10s|%-8s|%-10s", dnspodDomain.getId(), dnspodDomain.getStatus(),
						dnspodDomain.getRemark()));
			}
		} else if ("record".equals(command.toLowerCase())) {
			if (args.length < 3) {
				System.out.println("参数错误");
				return;
			}
			Integer domainId = Integer.parseInt(args[2]);

			List<DnspodRecord> recondList = IMDDNSRecordController.getRecondList(domainId);
			if (recondList == null) {
				return;
			}
			System.out.println(String.format("%-10s|%-15s|%-10s|%-20s", "记录ID", "主机记录", "解析线路", "记录值"));
			for (int i = 0; i < recondList.size(); i++) {
				DnspodRecord dnspodRecord = recondList.get(i);
				System.out.println(String.format("%-10s|%-15s|%-10s|%-20s", dnspodRecord.getId(),
						dnspodRecord.getName(), dnspodRecord.getLine(), dnspodRecord.getValue()));
			}
		} else if ("ddns".equals(command.toLowerCase())) {
			if (args.length < 4) {
				System.out.println("参数错误");
				return;
			}
			Integer domainId = Integer.parseInt(args[2]);
			Integer recordId = Integer.parseInt(args[3]);
			ddns(domainId, recordId);
		} else {
			showTip();
		}

	}

	public static void showTip() {
		StringBuilder builder = new StringBuilder();
		builder.append("user yourtoken // 获取用户信息 \r\n");
		builder.append("domain yourtoken // 获取域名列表 \r\n");
		builder.append("record yourtoken domainId // 获取解析记录列表 \r\n");
		builder.append("ddns yourtoken domainId recordId // 开启动态域名解析 \r\n");
		System.out.println(builder.toString());
		return;
	}

	private static void ddns(Integer domainId, Integer recordId) {
		DnspodRecord dnspodRecord = IMDDNSRecordController.getRecordInfo(domainId, recordId);
		if (dnspodRecord == null) {
			return;
		}
		UpdateRecordModel model = new UpdateRecordModel();
		model.setDomainId(domainId);
		model.setRecordId(recordId);
		model.setRecordLineId(dnspodRecord.getLine_id());
		model.setSubDomain(dnspodRecord.getName());
		UpdateRecordModel.addJobInfo(model);
		QuartzSimpleManager.addJob("update", RecordUpdateJob.class, "0 0 * * * ?");
	}

}
